package com.silong.swing.myWindows;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

/**
 * Created by 龙哥 on 2022-01-17 15:38
 * Silong Software.
 * 复选框
 */

public class CheckBoxFrame extends JFrame {

    JButton button1;
    JButton button2;
    JButton btnSetState;

    JLabel lblInfo;
    JCheckBox chkConfirm;
    JCheckBox chkCities[];
    String cities[]; //定义城市名称数组

    public CheckBoxFrame(){
        init();
    }

    private void init(){

        button1 = new JButton("确认");
        button2 = new JButton("选择");
        btnSetState = new JButton("设置复选框状态");

        cities= new String[]{"上海","广州","北京","合肥","香港"};
        //初始化部件
        lblInfo = new JLabel("请选择你去过的城市");
        lblInfo.setLocation(50,200); //设置按钮位置
        lblInfo.setSize(300,40); //设置按钮大小
        lblInfo.setFont(new Font("黑体", Font.BOLD, 20));

        chkConfirm = new JCheckBox("同意并且接受以上条款。");
        chkConfirm.setLocation(50,50); //设置按钮位置
        chkConfirm.setSize(300,40); //设置按钮大小
        chkConfirm.setFont(new Font("黑体", Font.BOLD, 20));

        Container window = this.getContentPane();
        this.setSize(800, 600);
        this.setLocationRelativeTo(null); //距中
        this.setTitle("自定义控件大小位置");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //退出窗口关闭程序
        //设置布局为空，优点:摆放灵活，缺点：代码量大，自适应能力差。
        this.setLayout(null);

        chkCities = new JCheckBox[cities.length];
        for (int i = 0; i < chkCities.length; i++) {
            chkCities[i] = new JCheckBox(cities[i]);
            chkCities[i].setLocation(50 + (i*70),240); //设置按钮位置
            chkCities[i].setSize(70,40); //设置按钮大小
            chkCities[i].setFont(new Font("黑体", Font.BOLD, 20));
            window.add(chkCities[i]);
        }

        //自定义按钮的位置 ，大小
        button1.setLocation(80,100); //设置按钮位置
        button1.setSize(80,40); //设置按钮大小
        button1.setFont(new Font("微软件雅黑", Font.PLAIN, 20));
        button1.setEnabled(false); //缺省按钮无效
        //选择中 chkConfirm时按钮有效，否则无效
        /*chkConfirm.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                boolean isCheck = chkConfirm.isSelected();//给出当前的选择状态
                button1.setEnabled(isCheck); //设置按钮的状态
            }
        });*/
        //或者使用 chkConfirm中的 Itemevent事件，效果与 ActionListener
        chkConfirm.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                button1.setEnabled(chkConfirm.isSelected());
            }
        });

        button2.setBounds(80, 300, 80, 40); //一次性设置位置与大小
        button2.setFont(new Font("微软件雅黑", Font.PLAIN, 20));
        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
               //给出用户选择的城市
                String city = "";
                for (int i = 0; i < chkCities.length; i++) {
                    if(chkCities[i].isSelected()){
                        city = city + chkCities[i].getText() + " ";
                    }
                }
                System.out.println("您选择的城市是：" + city);
            }
        });

        //设置复选框状态为选择或不选择
        btnSetState.setLocation(250,100); //设置按钮位置
        btnSetState.setSize(200,40); //设置按钮大小
        btnSetState.setFont(new Font("黑体", Font.BOLD, 20));
        btnSetState.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                chkConfirm.setSelected(!chkConfirm.isSelected()); //每次点击都进行取反
            }
        });
        window.add(button1);
        window.add(button2);
        window.add(chkConfirm);
        window.add(lblInfo);
        window.add(btnSetState);
    }

    public static void main(String[] args) {
        CheckBoxFrame myFrame = new CheckBoxFrame();
        myFrame.setVisible(true);
    }
}
